package com.ewanghuiju.app.ui.my.activity;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.FansInfoContract;
import com.ewanghuiju.app.model.bean.local.FansInfoBean;
import com.ewanghuiju.app.model.bean.response.TeamInfoResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.presenter.my.FansInfoPresenter;
import com.ewanghuiju.app.ui.my.adapter.FansInfoListAdapter;
import com.ewanghuiju.app.util.ClipboardUtils;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.TimeUtils;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 粉丝详情页面
 * @time 2020/2/24
 */
public class FansInfoActivity extends BaseActivity<FansInfoPresenter> implements FansInfoContract.View {
    @BindView(R.id.layout_main)
    RelativeLayout layoutMain;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_sf_mark)
    TextView tvSfMark;
    @BindView(R.id.riv_head)
    RoundedImageView rivHead;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerview;
    @BindView(R.id.tv_today)
    TextView tvToday;
    @BindView(R.id.tv_yesterday)
    TextView tvYesterday;
    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_head_center)
    ImageView ivHeadCenter;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.mtoolbar)
    Toolbar mtoolbar;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.tv_fans_num)
    TextView tvFansNum;
    //    @BindView(R.id.layout_tittle)
//    LinearLayout layoutTittle;
    @BindView(R.id.tv_regist_time)
    TextView tvRegistTime;
    @BindView(R.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R.id.tv_phone_no)
    TextView tvPhoneNo;
    @BindView(R.id.tv_phone_copy)
    TextView tvPhoneCopy;
    @BindView(R.id.tv_wx_no)
    TextView tvWxNo;
    @BindView(R.id.tv_wx_copy)
    TextView tvWxCopy;
    @BindView(R.id.tv_fanli_mark)
    TextView tvFanliMark;
    @BindView(R.id.tv_today_title)
    TextView tvTodayTitle;
    @BindView(R.id.tv_pintuan_mark)
    TextView tvPintuanMark;
    @BindView(R.id.tv_today_shouyi_title)
    TextView tvTodayShouyiTitle;
    @BindView(R.id.relativelayout_top)
    RelativeLayout relativelayoutTop;
    @BindView(R.id.linearlayout_top)
    LinearLayout linearlayoutTop;

    private FansInfoListAdapter adapter;

    private int currentPage = 1;
    private String searchText = "";

    private String fansId;

    private UserInfoResponBean mUserInfoResponBean;

    @Override
    protected int getLayout() {
        return R.layout.activity_fans_info;
    }

    @Override
    protected void initEventAndData() {
        fansId = getIntent().getIntExtra(Constants.TEAM_FANS_ID, 0) + "";
        mToolbar.setBackground(null);
        mToolbar.setNavigationIcon(R.mipmap.icon_fan_white);
        tvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[]{Color.parseColor("#24242E"), Color.parseColor("#2E2E3A")});
        layoutMain.setBackground(gd);
//        GradientDrawable gdsf = (GradientDrawable) tvSfMark.getBackground();
//        gdsf.setColor(ContextCompat.getColor(mContext, R.color.color_a1a0a7));

        initRecylerView();
        intRefreshLayout();
    }

    private void intRefreshLayout() {
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage++;
                mPresenter.getData(currentPage, searchText, fansId);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getData(currentPage, searchText, fansId);
            }
        });
        currentPage = 1;
        refreshLayout.resetNoMoreData();
        mPresenter.getData(currentPage, searchText, fansId);
    }

    private void initRecylerView() {
        recyclerview.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new FansInfoListAdapter(R.layout.item_fans_info_list);
        adapter.setHasStableIds(true);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FansInfoBean fansInfoBean = (FansInfoBean) adapter.getItem(position);
                new StartActivityUtil(mContext, FansInfoActivity.class).putExtra(Constants.TEAM_FANS_ID,
                        fansInfoBean.getId())
                        .startActivity(true);
            }
        });
        recyclerview.setAdapter(adapter);
    }

    @OnClick({R.id.tv_phone_copy, R.id.tv_wx_copy})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_phone_copy:
                //复制手机号
                if (mUserInfoResponBean != null && !TextUtils.isEmpty(mUserInfoResponBean.getMobile())) {
                    ClipboardUtils.copyText(mUserInfoResponBean.getMobile(), "复制成功");
                }
                break;
            case R.id.tv_wx_copy:
                //复制微信号
                if (mUserInfoResponBean != null && !TextUtils.isEmpty(mUserInfoResponBean.getWx_card())) {
                    ClipboardUtils.copyText(mUserInfoResponBean.getWx_card(), "复制成功");
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected int getTitleIntText() {
        return R.string.fans_info_title;
    }

    @Override
    protected void intSmartRefreshLayout() {

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(TeamInfoResponBean bean) {
        //获取信息成功
        LoadingDialogUtils.dismissDialog_ios();

        if (bean == null) {
            return;
        }

        UserInfoResponBean userInfoResponBean = bean.getUserinfo();
        this.mUserInfoResponBean = userInfoResponBean;
        if (userInfoResponBean != null) {
            relativelayoutTop.setVisibility(View.VISIBLE);
            linearlayoutTop.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(userInfoResponBean.getHeadimg()).placeholder(R.mipmap.icon_default_head).error(R.mipmap.icon_default_head).into(rivHead);
            tvNickname.setText(StringUtil.getLengthTextByStr(userInfoResponBean.getNickname(), 5));
            tvOrderNumber.setText(TextUtils.isEmpty(userInfoResponBean.getBuy_num()) ? "" : userInfoResponBean.getBuy_num());
            tvFansNum.setText(TextUtils.isEmpty(userInfoResponBean.getDirectly_fans()) ? "0" : userInfoResponBean.getDirectly_fans());
            tvSfMark.setText(StringUtil.getDailiInfo(mUserInfoResponBean.getIs_daili()));
            String createTime = userInfoResponBean.getCreate_time();
            if (TextUtils.isEmpty(createTime)) {
                createTime = "";
            } else {
                createTime = "注册时间：" + TimeUtils.millis2String(TimeUtils.string2Millis(createTime), "yyyy.MM.dd");
            }
            tvRegistTime.setText(createTime);

            String phoneNum = "";
            if (userInfoResponBean.getIs_mobile() == 1) {
                phoneNum = userInfoResponBean.getMobile();
            } else {
                phoneNum = StringUtil.getFormatePhone(userInfoResponBean.getMobile());
            }
            tvPhoneCopy.setText(TextUtils.isEmpty(phoneNum) ? "未设置" : phoneNum);

            tvWxCopy.setText(TextUtils.isEmpty(userInfoResponBean.getWx_card()) ? "未设置" : userInfoResponBean.getWx_card());

            tvToday.setText(TextUtils.isEmpty(userInfoResponBean.getJin()) ? "0" : userInfoResponBean.getJin());
            tvYesterday.setText(TextUtils.isEmpty(userInfoResponBean.getZuo()) ? "0" : userInfoResponBean.getZuo());
            tvMonth.setText(TextUtils.isEmpty(userInfoResponBean.getMonth()) ? "0" : userInfoResponBean.getMonth());
        } else {
            relativelayoutTop.setVisibility(View.GONE);
            linearlayoutTop.setVisibility(View.GONE);
        }

        List<FansInfoBean> list = bean.getList();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE_TWO) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            adapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE_TWO) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            adapter.addData(list);
        }
    }

    @Override
    public void showContentError() {
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
    }
}
